﻿Imports System.Web
Imports System.Web.Services

Public Class chatHandler
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Try
            context.Request.ValidateInput()
            Dim v = context.Request.QueryString("type")
        Catch ex As Exception

        End Try





        Select Case context.Request.QueryString("type")
            Case "login"
                Dim u = context.Request("u")
                Dim p = context.Request("p")
                Dim objUser As Chat.User = Chat.User.Authenticate(u, p)

                If objUser IsNot Nothing Then
                    Dim objToReturn = New With {.UserID = objUser.ID, .UserName = objUser.Name, .IsAuthenticated = True, .IsAdmin = objUser.IsAdmin}
                    WriteJsonObject(context, objToReturn)
                Else
                    Dim objToReturn = New With {.IsAuthenticated = False}
                    WriteJsonObject(context, objToReturn)
                End If

            Case "postchat"
                Dim u = context.Request("u")
                Dim m = context.Request("m")

                Chat.PostChat(m, u)

                Dim objToReturn = New With {.Sent = True}
                WriteJsonObject(context, objToReturn)
            Case "fetch"
                Dim u = context.Request("u")
                Dim l = context.Request("l")

                'Threading.Thread.Sleep(30000)



                Dim objToReturn = New With {.Messages = New List(Of Object), .LastMessageID = l}

                Dim lstMesssages = Chat.GetChats(l)

                Dim lastmessageID = (From t In lstMesssages Order By t.ID Select t.ID).LastOrDefault

                objToReturn.LastMessageID = lastmessageID

                For Each message In lstMesssages
                    objToReturn.Messages.Add(New With {.Content = message.Content,
                                                       .UserName = message.User.Name,
                                                       .SentAtString = message.SentAt.ToString("dd-MMM-yyyy HH:mm:ss"),
                                                       .SentAt = message.SentAt.ToUniversalTime.ToString("o")})
                Next

                WriteJsonObject(context, objToReturn)

            Case Else
                context.Response.ContentType = "text/plain"
                context.Response.Write("Hello World!")
        End Select



    End Sub

    Private Shared Sub WriteJsonObject(ByVal context As HttpContext, ByVal objToWrite As Object)
        Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
        'js.Serialize(objToWrite)
        context.Response.ContentType = "json"
        context.Response.Write(js.Serialize(objToWrite))
        context.Response.End()
    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property


End Class